//
//  HelpingCenterVC.m
//  MyNewProject
//
//  Created by sun on 2019/2/12.
//  Copyright © 2019 sun. All rights reserved.
//

#import "HelpingCenterVC.h"
#import "XMJSWebViewController.h"
#import "KeFuViewController.h"
#import "WKWebViewController.h"

#pragma mark -- mark 数据相关
#import "HelpingCenterModel.h"

@interface HelpingCenterView : UIView
@property (nonatomic, copy) void(^buttonClickBlock)(NSInteger);
- (instancetype)initWithDic:(NSDictionary *)dic;
@end
@implementation HelpingCenterView

- (instancetype)initWithDic:(NSDictionary *)dic {
    if (self = [super initWithFrame:CGRectZero]) {
        NSString *title = dic[@"title"];
        NSString *titleImageName = dic[@"image"];
        NSArray *titles = dic[@"titles"];
        
        UIView *leftView = [[UIView alloc] init];
        leftView.backgroundColor = [UIColor whiteColor];
        [self addSubview:leftView];
        [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.mas_offset(0);
            make.width.mas_equalTo(self.mas_width).multipliedBy(90.0/375);
        }];
        UIImageView *imageView = [[UIImageView alloc] init];
        [leftView addSubview:imageView];
        [imageView sd_setImageWithURL:[NSURL URLWithString:titleImageName]];
        UILabel *label = [[UILabel alloc] init];
        label.text = title;
        label.textColor = SKColorWithHex(0x888888);
        label.font = SKFONT_Medium(12);
        [leftView addSubview:label];
        if (titles.count <= 1) {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_offset(0);
                make.centerY.mas_offset(-10);
                make.size.mas_equalTo(CGSizeMake(40, 40));
                make.top.mas_offset(10);
            }];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(imageView);
                make.top.mas_equalTo(imageView.mas_bottom).mas_offset(6);
                make.bottom.mas_offset(-10);
            }];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:titles[0] forState:UIControlStateNormal];
            [btn setTitleColor:SKColorWithHex(0x333333) forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor whiteColor];
            btn.titleLabel.font = SKFONT_Medium(13);
            btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, 16, 0, btn.titleEdgeInsets.right - 16);
            [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            btn.tag = 520;
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(leftView.mas_right).mas_offset(0.5);
                make.right.mas_offset(0);
                make.top.bottom.mas_equalTo(leftView);
                make.top.bottom.mas_offset(0);
            }];
        }else {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_offset(0);
                make.centerY.mas_offset(-10);
                make.size.mas_equalTo(CGSizeMake(40, 40));
            }];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(imageView);
                make.top.mas_equalTo(imageView.mas_bottom).mas_offset(6);
            }];
            for (int i = 0; i < titles.count; i++) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setTitle:titles[i] forState:UIControlStateNormal];
                [btn setTitleColor:SKColorWithHex(0x333333) forState:UIControlStateNormal];
                btn.backgroundColor = [UIColor whiteColor];
                btn.titleLabel.font = SKFONT_Medium(13);
                btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
                btn.titleEdgeInsets = UIEdgeInsetsMake(0, 16, 0, btn.titleEdgeInsets.right - 16);
                [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:btn];
                btn.tag = 520 + i;
                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(leftView.mas_right).mas_offset(0.5);
                    make.right.mas_offset(0);
                    make.height.mas_equalTo(42);
                    make.top.mas_offset(i * (42.0 + 0.5));
                    if (i == 0) {
                        make.top.mas_equalTo(leftView);
                    }else if (i == titles.count - 1) {
                        make.bottom.mas_offset(0);
                        make.bottom.mas_equalTo(leftView);
                    }
                }];
            }
        }
    }
    return self;
}

- (void)buttonClick:(UIButton *)sender {
    if (self.buttonClickBlock) {
        self.buttonClickBlock(sender.tag - 520); }
}
@end

@interface HelpingCenterVC () {
    NSArray *_arr;
}
@property (nonatomic, strong) NSArray <HelpingCenterDataModel *>* dataArray;
@end
@implementation HelpingCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.gk_navTitle = @"帮助中心";
    [self requestHelpingData];
}

/**
 刷新数据
 */
- (void)reloadContentViews {
    /*
    NSArray *arr = @[
                     @{
                         @"title":@"购物相关",
                         @"image":@"mine_help_about_gouwu_img",
                         @"titles":@[@"发货时间", @"发货快递公司", @"运费问题", @"怎么联系商家"]
                         },
                     @{
                         @"title":@"拍卖相关",
                         @"image":@"mine_help_about_paimai_img",
                         @"titles":@[@"拍卖误出价怎么办", @"参与拍卖的保证金多久退回"]
                         },
                     @{
                         @"title":@"售后相关",
                         @"image":@"mine_help_about_shouhou_img",
                         @"titles":@[@"关于退货", @"申请退货退款", @"申请未收到货仅退款", @"退货运费怎么算", @"商家售假怎么办"]
                         },
                     @{
                         @"title":@"财务相关",
                         @"image":@"mine_help_about_caiwu_img",
                         @"titles":@[@"关于提现", @"退货退款多久到账", @"关于拍卖保证金", @"大额支付不成功怎么办"]
                         },
                     @{
                         @"title":@"其他相关",
                         @"image":@"mine_help_about_others_img",
                         @"titles":@[@"拍卖误出价了怎么办", @"参与拍卖的保证金多久退回"]
                         },
                     ];
     */
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.showsVerticalScrollIndicator = false;
    scrollView.showsHorizontalScrollIndicator = false;
    scrollView.backgroundColor = SKColorWithHex(0xF7F7F7);
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(SKTopBar_Height);
        make.bottom.mas_offset(-SKBottomSafe_Height);
        make.width.mas_equalTo(self.view);
    }];
    UIView *topLine = [[UIView alloc] init];
    [scrollView addSubview:topLine];
    
    
    HelpingCenterView *temView;
    for (int i = 0; i < _arr.count; i++) {
        NSDictionary *info = _arr[i];
        HelpingCenterView *view = [[HelpingCenterView alloc] initWithDic:info];
        SKWeakSelf;
        view.buttonClickBlock = ^(NSInteger index) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:i];
            [weakSelf buttonClickWith:indexPath];
        };
        [scrollView addSubview:view];
        if (!temView) {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_offset(0);
                make.width.mas_equalTo(self.view);
                make.top.mas_offset(10);
            }];
        }else {
            if (i == (_arr.count - 1)) {
                [view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(temView.mas_bottom).mas_offset(10);
                    make.left.right.mas_offset(0);
                    make.width.mas_equalTo(self.view);
                    make.bottom.mas_offset(-10);
                }];
            }else {
                [view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.right.mas_offset(0);
                    make.width.mas_equalTo(self.view);
                    make.top.mas_equalTo(temView.mas_bottom).mas_offset(10);
                }];
            }
        }
        temView = view;
    }
    temView = nil;
}

#pragma mark -- mark 获取数据
- (void)dealwithData {
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:self.dataArray.count];
    for (HelpingCenterDataModel *model in self.dataArray) {
        NSString *title = model.title;
        NSString *image = model.img;
        NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:model.info.count];
        for (HelpingCenterDataContentModel *info in model.info) {
            NSString *name = info.title;
            [mArr addObject:name];
        }
        NSDictionary *dic = @{@"title":title,
                              @"image":image,
                              @"titles":[NSArray arrayWithArray:mArr]
                              };
        [arr addObject:dic];
    }
    _arr = [NSArray arrayWithArray:arr];
}
/**
 获取帮助中心数据
 */
- (void)requestHelpingData {
    SKWeakSelf;
    [YQNetworking getWithUrl:@"Message/helpCenter" params:@{} reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        HelpingCenterModel *model = [HelpingCenterModel modelWithDictionary:response];
        if ([model  isSuccess]) {
            weakSelf.dataArray = model.data;
            [weakSelf dealwithData];
            [weakSelf reloadContentViews];
        }else {
            [weakSelf showMessage:model.msg];
        }
    } failBlock:^(NSError *error) {
        [weakSelf debugShowMessage:error.localizedDescription];
    }];
    
}


#pragma mark -- mark EVENT

- (void)buttonClickWith:(NSIndexPath *)indexPath {
    HelpingCenterDataModel *data = self.dataArray[indexPath.section];
    HelpingCenterDataContentModel *info = data.info[indexPath.row];
    XMJSWebViewController * webVC = [[XMJSWebViewController alloc]init];
    webVC.type = WebType_H5_content;
    webVC.titleString = info.title;
    webVC.fileUrl = info.html;
    [self.navigationController pushViewController:webVC animated:YES];
}

@end
